import {
  getGuideBookFileInfoByPage,
  addGuideBookFileInfo,
  updateGuideBookFileInfo,
  delGuideBookFileInfo,
  exportGuideBookFileInfo,
  viewBaseInfo,
  getGuideBookFileInfoPreviewUrl2,
  downloadGuideBookFile2,
  getGuideBookFileInfoBybatchProductId,
} from '@/services/product/guideBookFileInfo';
import { Ellipsis } from '@/components/Ellipsis';
import { PlusOutlined, UploadOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree, Upload } from 'antd';
import { sortTran, filterTran, filterTranNew } from '@/utils/requestUtil';
import React, { useEffect, useRef, useState } from 'react';
import { ModalForm, ProFormText } from '@ant-design/pro-components';
import moment from 'moment';
const GuideBookFileInfo: React.FC<{}> = (props) => {
  const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
  const ref = useRef(null);
  const [file, setFile] = useState();
  const current_id = props.theid;
  const current_batchproductid = props.batchproductid;

  const columns: ProColumns[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'index',
      width: 50,
    },
    // {
    //   title: '文件编号',
    //   dataIndex: 'guideFileCode',
    //  align: 'center',sorter: true,
    //   width: 120,
    //   render: (text) => <>{Ellipsis(text)}</>,
    //   search: false,
    //
    // },
    {
      title: '文件名称',
      dataIndex: 'guideFileName',
      align: 'center',
      sorter: true,
      width: 120,
      render: (text) => <>{Ellipsis(text)}</>,
    },
    // {
    //   title: '文件链接',
    //   dataIndex: 'fileUrl',
    //  align: 'center',sorter: true,
    //   width: 120,
    //   render: (text) => <>{Ellipsis(text)}</>,
    //   search: false,
    // },
    {
      title: '指导书说明',
      dataIndex: 'instruction',
      align: 'center',
      sorter: true,
      search: false,
      width: 150,
      render: (text) => <>{Ellipsis(text)}</>,
    },
    {
      title: '上传人',
      dataIndex: 'uploaderName',
      align: 'center',
      sorter: true,
      width: 80,
      render: (text) => <>{Ellipsis(text)}</>,
    },
    {
      title: '上传时间',
      dataIndex: 'uploadTime',
      align: 'center',
      sorter: true,
      width: 100,
      render: (text) => <>{Ellipsis(text)}</>,
      search: false,
    },
    // {
    //   title: '创建日期', //表头显示的名称
    //   search: false,
    //   dataIndex: 'createTime',
    // },
    // {
    //   title: '创建人', //表头显示的名称
    //   search: false,
    //   dataIndex: 'createName',
    // },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      fixed: 'right',
      align: 'center',
      width: 110,
      render: (text, record, _, action) => [
        <a
          key="editKey"
          onClick={async () => {
            const filetype = record.guideFileName.split('.').pop();
            if (filetype == 'pdf' || filetype == 'png' || filetype == 'jpg') {
              const result = await getGuideBookFileInfoPreviewUrl2({
                guideFileCode: record.guideFileCode,
                guideFileName: record.guideFileName,
                bucketName: record.bucketName,
                batchProductId: current_batchproductid,
              });
              const w = window.open('about:blank');
              w.location.href = result.message;
            } else {
              var para={};
              if (record.id == null){
                para={ guideFileCode: record.guideFileCode};
              }else{
                para={guideFileCode: record.guideFileCode,id: record.id}              
              }
              downloadGuideBookFile2(para).then((res) => {
                const aLink = document.createElement('a');
                document.body.appendChild(aLink);
                aLink.style.display = 'none';
                aLink.href = window.URL.createObjectURL(res);
                aLink.setAttribute('download', `${record.guideFileName}`);
                aLink.click();
                document.body.removeChild(aLink);
              });
            }
          }}
        >
          预览下载
        </a>,
        <a
          key="deleteKey"
          hidden={current_id != null}
          onClick={() => {
            Modal.confirm({
              title: '删除',
              content: '确定要删除吗',
              okText: '确认',
              cancelText: '取消',
              onOk: async () => {
                await delGuideBookFileInfo({ id: record.id });
                actionRef.current.reload();
              },
            });
          }}
        >
          删除
        </a>,
      ],
    },
  ];

  const onFilesChange = async (file) => {
    setFile(file);
  };

  const beforeUpload = (file) => {
    setFile(file);
    return false;
  };

  return (
    <>
      <ProTable
        headerTitle=""
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        formRef={ref}
        search={
          current_id != null
            ? false
            : {
                labelWidth: 'auto',
                // defaultCollapsed: true,
              }
        }
        toolBarRender={(action, { selectedRows }) => [
        ]}
        request={async (params = {}, sort, filter) => {
          // const res = {...params,batchProductId: current_batchproductid};
          // const sorters = sortTran(sort);
          // const filters = filterTranNew(res);
          const result = await getGuideBookFileInfoBybatchProductId(current_batchproductid);
          return {
            data: result.data,
            total: result.data.totalElements,
            success: result.success,
          };
        }}
        columns={columns}
        scroll={{ x: 1200 }}
        rowSelection={{}}
      />
    </>
  );
};
export default GuideBookFileInfo;
